package com.tensquare.base.controller;

import com.alibaba.fastjson.JSON;
import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.Result00;
import entity.StatusCode;
import jdk.nashorn.internal.scripts.JS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by WF on 2020/7/21 15:21
 */
@RestController
@RequestMapping("label")
@CrossOrigin
@RefreshScope           //让自己定义的配置也会随着bus的改释而改变
public class LabelController {
    @Autowired
    private LabelService labelService;

    @Value("${ip}")
    private String port;

    //1.列表所有的标签
    @GetMapping
    public Result findAll(){
        //1.1)查询所有的标签
        List<Label> labels = labelService.findAll();
        //String s = JSON.toJSONString(labels);
//        System.out.println("s = " + s);
//        List<Label> labels1 = JSON.parseArray(s, Label.class);
//        System.out.println("labels1 = " + labels1);
        //1.2)返回
        return new Result(true, StatusCode.OK,"查询标签列表成功！--->" + port ,labels);
    }
    //2.添加标签
    @PostMapping
    public Result add(@RequestBody Label label){
        //2.1)添加标签
        labelService.add(label);
        //2.2)返回
        return new Result(true,StatusCode.OK,"添加标签成功！");
    }
    //3.修改标签
    @PutMapping("{labelId}")
    public Result update(@RequestBody Label label,@PathVariable String labelId){
        //3.1)将要修改的lable与labelid进行绑定
        label.setId(labelId);
        //3.2)完成修改
        labelService.update(label);
        //3.3)返回
        return new Result(true,StatusCode.OK,"修改标签成功！");
    }
    //4.根据标签id来删除标签
    @DeleteMapping("{labelId}")
    public Result delete(@PathVariable String labelId){
        //4.1)完成修改
        labelService.delete(labelId);
        //4.2)返回
        return new Result(true,StatusCode.OK,"删除标签成功！");
    }
    //5.根据id查询标签
    @GetMapping("{labelId}")
    public Result findById(@PathVariable String labelId){
        //5.1)根据id查询标签
        Label label = labelService.findById(labelId);
        //5.2)返回
        return new Result(true,StatusCode.OK,"查询标签成功！",label);
    }
    //6.条件查询带分页
    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody Label label,@PathVariable int page,@PathVariable int size){
        //6.1)分页带条件查询
        PageResult<Label> pageResult = labelService.search(label,page,size);
        //6.2)返回
        return new Result(true,StatusCode.OK,"条件带分页查询成功！",pageResult);
    }

}
